﻿@using KC.MVC.Core.Constants
@using KC.Web.Account.Constants
@using Microsoft.AspNetCore.Authorization
@inject IAuthorizationService AuthorizationService
@{
    //菜单角色
    var menuId = ViewBag.menuId;
}

<div class="easyui-layout" style="height: 100%;">
    <div id="p" data-options="region:'west',collapsible:false," title="菜单列表" style="width:300px;padding:1px">
        <div style="background-color: #F2F2F2; padding: 4px; display: none;">
            <a href="#" onclick="reloadMenu();" class="easyui-linkbutton ListToolStartBtn" data-options="iconCls:'icon-search'">查询</a>
        </div>
        <div id="menuInRole"></div>
    </div>
    <div data-options="region:'center'">
        <div id="permissionsList" name="permissions" style="padding:1px; height:98%;" data-options="title:'角色菜单',refreshable:false">
            <div id="toolbar" style="padding: 5px; height: auto">
                <div style="float: right; display: none;">
                    @*@Html.DropDownList("searchAppId", (IEnumerable<SelectListItem>)ViewBag.AppliationList, new { @id = "searchAppId", @class = "easyui-combobox", @style = "width: 200px;height:26px;" })*@
                    <a href="javascript:void(0)" onclick="reloadPermission();" class="easyui-linkbutton" data-options="iconCls:'fa fa-search'">查询</a>
                </div>
                @if ((await AuthorizationService.AuthorizeAsync(User, "6862116B-CA67-44DA-9E6A-D5C05DA7F964")).Succeeded)
                {
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="fa fa-plus" onclick="SaveMenu()">保存</a>
                }
                <a id="closeCurrentBtn" class="easyui-linkbutton" style="margin-left: 5px" onclick="closeCurrentTab()" iconCls="fa fa-times" href="javascript:void(0)">关闭当前页</a>
            </div>
            <table id="RoleData" title="角色菜单"></table>
        </div>
    </div>
</div>

@section scripts
    {
    <script type="text/javascript">
        var preSelectMenuId = '@(menuId ?? 0)';
        var RoleData = $('#RoleData');
        var menuInRole = $('#menuInRole');
        var menuList = $('#menuList');
        $(function () {
            InitDataGrid(preSelectMenuId);
            LoadMenuData(preSelectMenuId);

        });

        //关闭当前页
        var closeCurrentUrl = '@Url.Action(ActionName.Menu.RoleInMenu, ControllerName.Menu)';
        function closeCurrentTab() {
            MainPage_PostMessage("closeSubPage", closeCurrentUrl);
        }
        //单击查询重新加载菜单
        function reloadMenu() {
            preSelectMenuId = '';
            menuInRole.treegrid('clearSelections');
            LoadMenuData();
        }
        //加载角色列表
        var getMenuInRoleUrl = '@Url.Action(ActionName.Menu.GetRoleInMenu, ControllerName.Menu)';
        function InitDataGrid(menuId) {
            var $dataGrid = RoleData.datagrid({
                url: getMenuInRoleUrl + "?id=" + menuId,
                idField: 'RoleId',
                toolbar: '#toolbar',
                striped: true,
                pagination: false,
                fitColumns: true,
                rownumbers: true,
                singleSelect: false,
                nowrap: false,
                fit: true,
                lines: true,
                showFooter: false,
                checkbox: true,
                columns: [
                    [
                        { field: 'RoleId', checkbox: true, title: 'ID', align: 'left' },
                        { field: 'DisplayName', title: '角色名称', width: '20%', align: 'left' },
                        //{ field: 'RoleName', title: '角色名称', width: '15%', align: 'left' },
                        { field: 'IsSystemRole', title: '是否为系统角色', width: '20%', align: 'left', formatter: Common.BoolFormatter },
                        //{ field: 'ApplictionName', title: '应用名称', width: '20%', align: 'left' },
                        { field: 'Description', title: '描述', width: '40%', align: 'left' }
                    ]
                ],
                onLoadSuccess: function (row) {
                    bindRole(row.rows);
                },
                onLoadError: function () {
                }
            });
        }

        //加载菜单
        var getDataUrl = '@Url.Action(ActionName.Menu.LoadMenuList, ControllerName.Menu)';
        function LoadMenuData(id) {
            $.easyui.loading({ msg: '正在加载数据，请稍等...' });
            $.ajax({
                url: getDataUrl, //所有菜单
                data: { searchValue: "" },
                async: false,
                type: "post",
                success: function (result) {
                    $.easyui.loaded();
                    InitMenuTree(result);
                },
                error: function () {
                    $.easyui.loaded();
                }
            });
        }

        function InitMenuTree(data) {
            menuInRole.treegrid({
                lines: true,
                idField: 'id',
                treeField: 'text',
                valueField: 'id',
                textField: 'text',
                columns: [
                    [
                        { field: 'text', title: '名称', width: 260, align: 'left' }
                    ]
                ],
                onSelect: function (index, row) {
                    if (index != null)
                        preSelectMenuId = index.id;
                    bindmenu(preSelectMenuId);
                }
            }).datalist('loadData', data);

            if (preSelectMenuId.length > 0)
                selectedTreeNode(data, preSelectMenuId);

            //menuInRole.treegrid('collapseAll');
            var node = menuInRole.treegrid("find", preSelectMenuId);
            if (node.ParentId != null)
                menuInRole.treegrid("expandAll", node.ParentId);
            else
                menuInRole.treegrid("expandAll", node.id);
        }

        function selectedTreeNode(data, preSelectTreeId) {
            if (data.length > 0) {
                $.each(data, function (index) {
                    //if (this.children.length == 0) {
                    if (this.id == preSelectTreeId) {
                        menuInRole.datalist('selectRow', preSelectTreeId);
                        return;
                    }
                    //}
                    selectedTreeNode(this.children, preSelectTreeId);
                });
            }
        }

        function bindmenu(roleId) {
            RoleData.datagrid('uncheckAll');
            $.easyui.loading({ msg: '正在加载数据，请稍等...' });
            $.ajax({
                url: getMenuInRoleUrl,
                data: { "id": roleId },
                type: "post",
                success: function (result) {
                    $.easyui.loaded();
                    bindRole(result);
                },
                error: function () {
                    $.easyui.loaded();
                }
            });
        }

        function bindRole(datas) {
            if (datas.length > 0) {
                $.each(datas, function () {
                    var $this = this;
                    if ($this.checked) {
                        var rows = RoleData.datagrid("getRows");
                        var row;
                        $.each(rows, function () {
                            if (this.RoleId == $this.RoleId) {
                                row = this;
                            }
                        });
                        RoleData.datagrid('checkRow', RoleData.datagrid("getRowIndex", row));
                    }
                });
            }
        }

        //保存菜单角色
        var saveMenuInRole = '@Url.Action(ActionName.Menu.SubmitRoleInMenu, ControllerName.Menu)';
        function SaveMenu() {
            var checkedNodes = RoleData.datagrid('getChecked');
            var newlist = [];

            if (checkedNodes.length > 0) {
                for (var i = 0; i < checkedNodes.length; i++) {
                    newlist.push(checkedNodes[i].RoleId);

                }
            }
            var item = menuInRole.datalist("getSelected");
            if (item == null || item.id == 0 || item.id == null) {
                $.messager.showErrorCenter('系统提示', "请选择左边的菜单", 1000);
                return;
            }
            $.easyui.loading({ msg: '正在保存数据，请稍等...' });
            $.ajax({
                url: saveMenuInRole,
                type: "post",
                data: { 'menuId': item.id, 'addList': newlist, "type": "menu" },
                success: function (data) {
                    $.easyui.loaded();
                    if (data.success) {
                        if (data.Result) {
                            $.messager.showInfoCenter('系统提示', '保存数据成功。', 1000);
                        } else {
                            $.messager.showErrorCenter('系统提示', '保存数据失败。', 1000);
                        }
                    } else {
                        $.messager.showErrorCenter('系统提示', data.message, 1000);
                    }
                },
                error: function () {
                    //var error = JSON.parse(e.responseText).Message;
                    $.easyui.loaded();
                    $.messager.showInfoCenter('系统提示', '抱歉,你不具有当前操作的权限');
                }
            });
        }
    </script>
}



